package com.coffee.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.coffee.core.anotation.LoginCheck;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.SkuTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.daili.api.req.DailiMerchantSkuListReq;
import com.coffee.daili.api.req.InsertMerchantReq;
import com.coffee.daili.api.result.DailiMerchantOrderResult;
import com.coffee.daili.api.result.DailiMerchantResult;
import com.coffee.daili.api.result.DailiOrderPageResult;
import com.coffee.daili.api.service.DailiMerchantService;
import com.coffee.daili.api.service.DailiMerchantSkuService;
import com.coffee.daili.api.service.DailiOrderService;
import com.coffee.sku.api.sku.result.MerchantSkuListResult;
import com.google.gson.JsonObject;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping
public class DailiController extends BaseController {
    @DubboReference
    private DailiMerchantService dailiMerchantService;
    @DubboReference
    private DailiOrderService dailiOrderService;
    @DubboReference
    private DailiMerchantSkuService dailiMerchantSkuService;

    @OperLog(operModul = "商户-分销订单笔数", operDesc = "分销订单笔数")
    @LoginCheck
    @RequestMapping(value = "/merchant/daili/order/count", method = {RequestMethod.POST})
    public Result<DailiMerchantOrderResult> orderInquiry(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        String date = jsonObject.getString("date");
        Integer dailiMerchantId = jsonObject.getInteger("dailiMerchantId");
        Result<DailiMerchantOrderResult> result = Result.success(null);
        DailiMerchantOrderResult dailiMerchantOrderResult = dailiOrderService.getDailiMerchantOrderResult(merchant.getUserId(), date, dailiMerchantId);
        result.setData(dailiMerchantOrderResult);
        return result;
    }

    @OperLog(operModul = "商户-分销员列表", operDesc = "分销员列表")
    @LoginCheck
    @RequestMapping(value = "/merchant/dalili/fenxiaoyuan/list", method = {RequestMethod.POST})
    public Result<Page<DailiMerchantResult>> daliliFenxiaoyuanList(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Result<Page<DailiMerchantResult>> result = Result.success(null);
        String date = jsonObject.getString("date");
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<DailiMerchantResult> listByMerchantId = dailiMerchantService.getListByMerchantId(merchant.getUserId(), date, pagerInfo);
        result.setData(listByMerchantId);
        return result;
    }

    @OperLog(operModul = "商户-分销员列表", operDesc = "分销员列表")
    @LoginCheck
    @RequestMapping(value = "/merchant/dalili/fenxiaoyuan/update", method = {RequestMethod.POST})
    public Result<String> daliliFenxiaoyuanUpdate(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Result<String> result = Result.success(null);
        Integer id = jsonObject.getInteger("id");
        Double proportion = jsonObject.getDouble("proportion");
        String isEnable = jsonObject.getString("isEnable");
        DailiMerchantResult update = new DailiMerchantResult();
        update.setId(id);
        update.setMerchantId(merchant.getUserId());
        update.setProportion(proportion);
        update.setIsEnable(isEnable);
        try {
            dailiMerchantService.updateByMerchant(update);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("success");
        return result;
    }

    @OperLog(operModul = "商户-商户列表", operDesc = "商户列表")
    @LoginCheck
    @RequestMapping(value = "/merchant/dalili/merchant/list", method = {RequestMethod.POST})
    public Result<Page<DailiMerchantResult>> daliliMerchantList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        LoginUser merchant = getMerchant();
        Result<Page<DailiMerchantResult>> result = Result.success(null);
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        String mobile = jsonObject.getString("mobile");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<DailiMerchantResult> listByMerchantId = dailiMerchantService.getMerchantListByMerchantId(mobile, merchant.getUserId(), pagerInfo);
        result.setData(listByMerchantId);
        return result;
    }

    @OperLog(operModul = "商户-商户列表", operDesc = "商户列表")
    @LoginCheck
    @RequestMapping(value = "/merchant/dalili/merchant/update", method = {RequestMethod.POST})
    public Result<String> daliliMerchantUpdate(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        LoginUser merchant = getMerchant();
        Result<String> result = Result.success(null);
        Integer id = jsonObject.getInteger("id");
        Double quota = jsonObject.getDouble("quota");
        DailiMerchantResult dailiMerchantResult = new DailiMerchantResult();
        dailiMerchantResult.setId(id);
        dailiMerchantResult.setMerchantId(merchant.getUserId());
        dailiMerchantResult.setQuota(quota);
        try {
            dailiMerchantService.updateQuota(dailiMerchantResult);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "商户-销售明细列表", operDesc = "销售明细列表")
    @LoginCheck
    @RequestMapping(value = "/merchant/daili/order/list", method = {RequestMethod.POST})
    public Result<Page<DailiOrderPageResult>> dailiOrderList(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Integer dailiMerchantId = jsonObject.getInteger("dailiMerchantId");
        String date = jsonObject.getString("date");
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Result<Page<DailiOrderPageResult>> result = Result.success(null);
        Page<DailiOrderPageResult> dailiOrderPageResultPager = dailiOrderService.getDailiOrderPageResultPager(date, merchant.getUserId(), dailiMerchantId, pagerInfo);
        result.setData(dailiOrderPageResultPager);
        return result;
    }

    @OperLog(operModul = "商户-商品列表", operDesc = "商品列表")
    @RequestMapping(value = "/merchant/dalili/sku/list", method = {RequestMethod.POST})
    public Result<Page<MerchantSkuListResult>> skuList(@RequestBody DailiMerchantSkuListReq req, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        Result<Page<MerchantSkuListResult>> result = Result.success(null);
        req.setSkuType(SkuTypeEnum.SKU.getCode());
        req.setIsDisable("N");
        req.setIsEnable("Y");
        req.setIsApprove("Y");
        req.setIsDelete("N");
        req.setMerchantId(merchant.getUserId());
        result.setData(dailiMerchantSkuService.findMerchantSkuList(req, pagerInfo));
        return result;
    }

    @OperLog(operModul = "商户-申请合伙人", operDesc = "商品列表")
    @RequestMapping(value = "/merchant/daili/apply", method = {RequestMethod.POST})
    public Result<String> apply(@RequestBody InsertMerchantReq req, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Result<String> result = Result.success(null);
        try {
            req.setMerchantId(merchant.getUserId());
            dailiMerchantService.apply(req);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }
}
